package com.yksj.healthtalk.ui.interestwall;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.TagEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.interestwall.CategoryChooseFragment.FragmentItemClickListener;
import com.yksj.healthtalk.utils.XmlCategoryParserUtils;

/**
 * 分类选择activity
 * @author crj
 *
 */
public class StairCategoryChooseFragment extends Fragment implements OnItemClickListener,FragmentItemClickListener,OnClickListener{
	/**
	 * 所有一级分类
	 */
	private List<TagEntity> stairData;
	private ListView stairListView;
	private CategoryChooseAdapter stairAdapter;
	/**
	 * fragment 管理类
	 */
	private FragmentManager manager;
	/**
	 * fragment attach的framelayout
	 */
	private ViewGroup viewGroup;
	/**
	 * 二级分类fragment
	 */
	private CategoryChooseFragment fragment;
	/**
	 * 监听器 
	 */
	private OnCategoryChooseListener onCategoryChooseListener;
	/**
	 *点击加载所有摄影馆分类图片
	 */
	private TextView categoryPhotoGallery;
	
	private TagEntity entity;
	/**
	 * 监听摄影馆按钮点击事件
	 */
	private OnPhotoGalleryButtonClick onPhotoGalleryButtonClick;
	
	public static StairCategoryChooseFragment getInstance() {
		StairCategoryChooseFragment f = new StairCategoryChooseFragment();
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initData();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.category_choose_main, container,false);
		initView(view);
		return view;
	}

	public void initView(View view) {
		initTitle(view);
		stairListView = (ListView) view.findViewById(R.id.category_listview);
		stairListView.setAdapter(stairAdapter);
		stairListView.setOnItemClickListener(this);
//		stairListView.setSelector(R.color.transparent);
		viewGroup = (ViewGroup) view.findViewById(R.id.fragmentContent);
//		categoryPhotoGallery = (TextView) view.findViewById(R.id.category_photogallery);
		categoryPhotoGallery.setOnClickListener(this);
	}
	
	public void initTitle(View view) {
		Button backButton = (Button) view.findViewById(R.id.title_back);
		backButton.setOnClickListener(this);
		backButton.setText("返 回");
		TextView textView = (TextView) view.findViewById(R.id.title_lable);
		textView.setText("分类选择");
		textView.setTextColor(getResources().getColor(R.color.charge_num_color));
	}

	public void initData() {
		//一级分类内的第一个item，点击之后不进入二级分类直接返回主界面刷新
		entity = new TagEntity("0", "兴趣墙");
		entity.setUpperId("0");
		stairData = new ArrayList<TagEntity>();
		stairData.add(entity);
		stairData.addAll(XmlCategoryParserUtils.getCategoryData(getResources(), R.xml.interestwall_class_store, 0));
		stairAdapter = new CategoryChooseAdapter(this.getActivity(), stairData);
		manager = this.getFragmentManager();
	}
	
	public static class CategoryChooseAdapter extends BaseAdapter	 {
		
		private LayoutInflater mInflater;
		private List<TagEntity> mList;

		public CategoryChooseAdapter(Context context, List<TagEntity> mList) {
			mInflater = LayoutInflater.from(context);
			this.mList = mList;
		}
		
		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(convertView==null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.category_choose_main_item, null);
				holder.categoryText = (TextView) convertView.findViewById(R.id.category_text);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.categoryText.setText(mList.get(position).getName());
			
			if(position==0) {
				convertView.setBackgroundResource(R.drawable.textview_background_up);
			} else if(position== getCount()-1) {
				convertView.setBackgroundResource(R.drawable.textview_background_down);
			} else {
				convertView.setBackgroundResource(R.drawable.textview_background_middle);
			}
			return convertView;
		}
		private class ViewHolder {
			TextView categoryText;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if(position==0) {
			hideFragment();
			onCategoryChooseListener.OnCategoryChoose(entity);
		} else {
			String ids = stairData.get(position).getId();
			viewGroup.setClickable(true);
			fragment = CategoryChooseFragment.newInstance(ids);
			FragmentTransaction transaction = manager.beginTransaction();
			transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
			transaction.add(R.id.fragmentContent, fragment);
			transaction.commitAllowingStateLoss();
		}
	}

	@Override
	public void fragmentClick(TagEntity entity1) {
		if(entity!=null) {
			hideFragment();
			onCategoryChooseListener.OnCategoryChoose(entity1);
		} else {
			viewGroup.setClickable(false);
		}
	}

	public void hideFragment() {
		FragmentManager manager = getParentFragment().getFragmentManager();
		manager.popBackStack();
		FragmentTransaction fragmentTransaction = manager.beginTransaction();
		if(this.isAdded()) {
			fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
			fragmentTransaction.remove(this);
		}
		onCategoryChooseListener.OnCategoryChoose(null);
		fragmentTransaction.commitAllowingStateLoss();
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.title_back:
			hideFragment();
			break;
		}
	}
	
	public void setCategoryChooseListener(OnCategoryChooseListener categoryChooseListener) {
		onCategoryChooseListener = categoryChooseListener;
	}
	
	public interface OnCategoryChooseListener {
		public void OnCategoryChoose(TagEntity entity);
	}
	
	public void setOnPhotoGalleryButtonClick(OnPhotoGalleryButtonClick onPhotoGalleryButtonClick) {
		this.onPhotoGalleryButtonClick = onPhotoGalleryButtonClick;
	}
	
	public interface OnPhotoGalleryButtonClick {
		public void onPhotoGalleryClick(); 
	}
	
}
